package vn.co.thn.web.demo_app_engine.api;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import vn.co.thn.web.demo_app_engine.bean.BeanBase;
import vn.co.thn.web.demo_app_engine.bean.UrlUpload;
import vn.co.thn.web.demo_app_engine.jpa.NewsFeed;
import vn.co.thn.web.demo_app_engine.json_object.NewFeedList;
import vn.co.thn.web.demo_app_engine.model.NewFeedModel;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.gson.Gson;

@Path("/newfeed")
public class ApiNewFeed {
	@Path("/list")
	@POST
	@Produces("application/json")
	public String list() {
		NewFeedModel model = new NewFeedModel();
		List<NewsFeed> list = model.getAll(NewsFeed.class,"DatePost asc");
		NewFeedList lst = new NewFeedList();
		lst.setLst(list);
		return new Gson().toJson(lst);
	}

	@Path("/url_upload")
	@Produces("application/json")
	@POST
	public String urlupload() {
		BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
		String url = blobstoreService.createUploadUrl("/api/newfeed/add");
		UrlUpload urlUpload = new UrlUpload();
		urlUpload.setUrl(url);
		return new Gson().toJson(urlUpload);

	}

	@SuppressWarnings("deprecation")
	@Path("/add")
	@POST
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	public String add(@Context HttpServletRequest request, @Context HttpServletResponse response, @FormParam("Image_Url") File file,
			@FormParam("Device_Id") String Device_Id, @FormParam("Content") String Content, @FormParam("lat") String lat, @FormParam("log") String log,
			@FormParam("Address") String Address, @FormParam("DatePost") String DatePost, @FormParam("Key_image") String Key_image) {
		BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
		Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(request);
		BlobKey blobKey = blobs.get("Image_Url");
		String key_image = blobKey.getKeyString();
		int port = request.getServerPort();
		String host;
		if (port == 80) {
			host = request.getServerName();
		} else {
			host = request.getServerName() + ":" + port;
		}
		NewFeedModel feedModel = new NewFeedModel();
		String date_post = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
		try {
			NewsFeed feed = BeanBase.RequestToObject(request, NewsFeed.class);
			feed.setImage_Url(request.getScheme() + "://" + host.replace("10.0.3.2", "localhost") + "/imageview?image_url=" + key_image);
			feed.setKey_image(key_image);
			feed.setDatePost(date_post);
			feedModel.Add(feed);
			return new Gson().toJson(feed);
		} catch (Exception e) {
			return null;
		}
	}
}
